import {Component, OnInit} from '@angular/core';
import {ExampleFlatNode, transformer, treeNode, TreeNode} from "./tree-node";
import {FlatTreeControl} from "@angular/cdk/tree";
import {MatTreeFlatDataSource, MatTreeFlattener} from "@angular/material/tree";

@Component({
  selector: 'app-side-nav-tree',
  templateUrl: './side-nav-tree.component.html',
  styleUrls: ['./side-nav-tree.component.less']
})
export class SideNavTreeComponent implements OnInit {

  treeNode: TreeNode[] = treeNode

  /**
   * 用于控制树节点所在层级和是否可以展开
   */
  treeControl = new FlatTreeControl<ExampleFlatNode>(
    node => node.level,
    node => node.expandable,
  );


  treeFlattener = new MatTreeFlattener(
    transformer,
    node => node.level,
    node => node.expandable,
    node => node.children,
  );

  dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);

  constructor() {
    this.dataSource.data = this.treeNode;
  }

  hasChild = (_: number, node: ExampleFlatNode) => node.expandable;

  ngOnInit(): void {
  }

  nodeClick(node: TreeNode){
    console.log(node)
  }

}
